﻿using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp;

namespace mozhi.smarterp.BasicData.Customers
{
    public class Customer : AggregateRootBase
    {
        public string Country { get; private set; }

        public string Region { get; private set; }

        public string MailAddress { get; private set; }

        public string PostalCode { get; private set; }

        public string Telephone { get; private set; }

        public List<CustomerContactItem> ContactItems { get; private set; }

        private Customer() { }

        public Customer(Guid id, string code, string name, bool activated) : base(id, code, name, activated)
        {
            ContactItems = new List<CustomerContactItem>();
        }

        public void ChangeRegionAndCountry(string country, string region)
        {
            Country = country;
            Region = region;
        }

        public void ChangeContactAddress(string mailAddress, string postalCode, string telephone)
        {
            MailAddress = mailAddress;
            PostalCode = postalCode;
            Telephone = telephone;
        }


        public void ChangeContactItems(List<CustomerContactItem> items)
        {
            if(items==null || items.Count==0)
            {
                ContactItems = null;
                return;
            }

            ContactItems = ContactItems ?? new List<CustomerContactItem>();

            items.ForEach(x =>
            {
                AddOrUpdateContactItem(x);
            });

            var itemIds = items.Select(item => item.Id);

            var deleted = ContactItems.Where(x=>!itemIds.Contains(x.Id)).ToList();

            deleted.ForEach(x => ContactItems.Remove(x));
        }

        public void AddOrUpdateContactItem(CustomerContactItem item)
        {
            var match = ContactItems.FirstOrDefault(x => x.Id == item.Id);

            if (match == null)
                ContactItems.Add(item);
            else
                match.Change(item.Name , item.Telephone , item.Mobilephone , item.Email);

        }

        #region
        protected override void Validate()
        {
            base.Validate();
        }
        #endregion
    }

    public class CustomerContactItem : EntityBase
    {
        public Guid CustomerId { get; private set; }

        public string Name { get; private set; }

        public string Telephone { get; private set; }

        public string Mobilephone { get; private set; }

        public string Email { get; private set; }

        private CustomerContactItem() { }

        public CustomerContactItem(Guid id, Guid customerId, string name, string telephone, string mobilephone, string email)
        {
            Id = id;
            CustomerId = customerId;
            Name = name;
            Telephone = telephone;
            Mobilephone = mobilephone;
            Email = email;

            Validate();
        }

        public void Change(string name , string telephone , string mobilephone, string email)
        {
            Name = name;
            Telephone = telephone;
            Mobilephone = mobilephone;
            Email = email;

            Validate();
        }

        #region
        private void Validate()
        {
            Check.NotDefaultOrNull<Guid>(CustomerId, "customerId");
            Check.NotNullOrEmpty(Name, "name");
        }

        #endregion
    }
}
